//
//  JWdistributResult.m
//  test
//
//  Created by 李彦宏 on 2018/2/1.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "JWdistributResult.h"
#import "DistributStuCell.h"//分配cell
#import "JWUserManager.h"
#import "SDUserModel.h"
#import "JWGRUsermodel.h"
#import "JWChooseGrSeachVC.h"//选择研究生搜索
@interface JWdistributResult ()
@property (nonatomic, strong) SDUserModel *SDmodel;
@property (nonatomic, strong) JWGRUsermodel *model;
@property (nonatomic, strong) NSMutableArray *SDarrylist;//学生列表
@property (nonatomic, strong) NSMutableArray *GRarrylist;//研究生列表
@end

@implementation JWdistributResult

- (void)viewDidLoad {
    [super viewDidLoad];
     self.SDmodel=[JWUserManager SDme];
    self.model=[JWUserManager Gme];
    [self setGRdate];
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(handleColorChange:) name:@"searchBarDidChange" object:nil];

   self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, Navigation_Bar_Height, ScreenWidth, ScreenHeight-Navigation_Bar_Height) style:UITableViewStyleGrouped];
    
    
}
-(void)handleColorChange:(NSNotification* )sender
{
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [EasyShowLodingView hidenLoding];
    });
    NSString *text = sender.userInfo[@"searchText"];
    NSLog(@"%@", text);
    NSDictionary *dic=@{
                        @"jwid":JWid,
                        @"jwpassword":JWpassword,
                        @"searchname":text,
                        };
    [[AFNetworkTool shareManager]postJSONWithUrl: JWSDsearch_list parmas:dic successData:^(id json) {
        //学生数据
        [self.SDarrylist removeAllObjects];
        __weak typeof(self) weakslf = self;
        NSArray *GRarry=json[@"studentlist"];
        for (int i=0; i<GRarry.count; i++){
            self.SDmodel=[SDUserModel yy_modelWithJSON:GRarry[i]];
            [JWUserManager saveSD:weakslf.SDmodel];
            [weakslf.SDarrylist addObject:weakslf.SDmodel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    } ];
    
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *head=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, CGFLOAT_MIN)];
    
    return head;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.SDarrylist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier=[NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
        DistributStuCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            cell=[[DistributStuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        [cell setChooseGRHandle:^{
            JWChooseGrSeachVC *vc=[[JWChooseGrSeachVC alloc]init];
            vc.tagsArray=[[NSMutableArray alloc]init];
            if(_GRarrylist.count<7){
                vc.tagsArray=[NSMutableArray arrayWithObjects:@"李彦宏",@"朱圳",@"严戈闲",@"赵延",@"田涛",@"肖欢欣",@"游鹤亭",@"陈炽", nil];
            }else{
                for (int i=0; i<7; i++) {
                    self.model=_GRarrylist[i];
                    [vc.tagsArray  addObject:self.model.name];
                }
                self.SDmodel = self.SDarrylist[indexPath.row];
                [JWUserManager saveSD:self.SDmodel];
                
            }
            [self.navigationController pushViewController:vc animated:YES];
            
        }];
        
        cell.searchModel = self.SDarrylist[indexPath.row];
        return cell;
    }



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.SDmodel = self.SDarrylist[indexPath.row];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.didSelectText(@"");
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -mark ————————————————研究生数据————————————————————
-(void)setGRdate{
    
    __weak typeof(self) weakslf = self;
    
    NSDictionary *dict=@{
                         @"jwid":JWid,
                         @"jwpassword":JWpassword,
                         @"page" :@"1",
                         };
  //  NSLog(@"--dict--%@", dict);
    //    参数  分页=>page 教务id=〉jwid ，秘码=>jwpassword
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_ChangeGR parame:dict successData:^(id json) {
        
        NSArray * arr = json[@"data"];
        
        for(int i=0;i<arr.count;i++){
            
            self.model = [JWGRUsermodel yy_modelWithJSON:arr[i]];
            [JWUserManager saveGR:self.model];
            
            [weakslf.GRarrylist addObject: self.model ];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  1;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *Foot=[[UIView alloc]init];
    Foot.backgroundColor=[UIColor whiteColor];
    
    return Foot;
}
-(NSMutableArray *)SDarrylist{
    if(!_SDarrylist){
        _SDarrylist=[[NSMutableArray alloc]init];
    }
    return _SDarrylist;
}
-(NSMutableArray *)GRarrylist{
    if(_GRarrylist==nil){
        _GRarrylist=[[NSMutableArray alloc]init];
    }
    return _GRarrylist;
    
}


@end
